/**
 * @author Daniel Monteiro
 * */

package br.odb.bzk3.midp;

import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;

import br.odb.bzk3.geometry.Actor;

public class World {
	private Sector sector;
	private Vector sectorList;

	// ------------------------------------------------------------------------------------------------------------
	public int getTotalSectors() {
		return sectorList.size();
	}

	// ------------------------------------------------------------------------------------------------------------
	public Sector getSector(int index) {
		return (Sector) sectorList.elementAt(index);
	}

	// ------------------------------------------------------------------------------------------------------------
	public String toString() {
		return String.valueOf(sectorList.size()) + " sector(s) loaded";

	}

	String getSubToken(String main, int token) {
		String work = main;
		String toreturn = null;
		while (token > -1) {
			if (token == 0) {
				if (work.indexOf(' ') != -1) {
					toreturn = work.substring(0, work.indexOf(' '));
				} else {
					toreturn = work;
				}
			}

			if (work.indexOf(' ') != -1) {
				work = work.substring(work.indexOf(' ') + 1);
			}

			token--;
		}

		return toreturn;
	}

	// ------------------------------------------------------------------------------------------------------------
	public World(InputStream FileContentStream, Vector iPeople) {
		sectorList = new Vector();

		try {
			if (FileContentStream != null && FileContentStream.available() != 0)
				internalize(FileContentStream, iPeople);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	// ------------------------------------------------------------------------------------------------------------
	public void internalize(InputStream FileContentStream, Vector iPeople) {
		this.sectorList.addElement(new Sector());

		// String[] result = FileContent.split("\n");
		// for (int x = 0; x < result.length; x++)
		// {
		// try
		// {
		// Parse(result[x],iPeople);
		// } catch (Exception ex)
		// {
		// ex.printStackTrace();
		// }
		// }
		String line;
		line = "";
		char ch;
		try {
			while (FileContentStream.available() != 0) {
				ch = (char) FileContentStream.read();
				if (ch == '\n') {
					line += " ";
					if (line.length() > 0)
						Parse(line, iPeople);

					line = "";
				} else {
					line += ch;
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// ------------------------------------------------------------------------------------------------------------
	private void Parse(String Sub, Vector people) {
		switch (Sub.charAt(0)) {
		case 'c':
			sector.SetColor(Integer.parseInt(getSubToken(Sub, 1)),
					Integer.parseInt(getSubToken(Sub, 2)),
					Integer.parseInt(getSubToken(Sub, 3)),
					Integer.parseInt(getSubToken(Sub, 4)));
			break;
		case 'p':
			sector.SetLinks(					
					plusOneIfNotZero( Integer.parseInt(getSubToken(Sub, 1)) ),
					plusOneIfNotZero( Integer.parseInt(getSubToken(Sub, 2)) ),
					plusOneIfNotZero( Integer.parseInt(getSubToken(Sub, 3)) ),
					plusOneIfNotZero( Integer.parseInt(getSubToken(Sub, 4)) ),
					plusOneIfNotZero( Integer.parseInt(getSubToken(Sub, 5)) ),
					plusOneIfNotZero( Integer.parseInt(getSubToken(Sub, 6)) ) );
			break;
		case 'l':
			sector.SetLinks(Integer.parseInt(getSubToken(Sub, 1)),
					Integer.parseInt(getSubToken(Sub, 2)),
					Integer.parseInt(getSubToken(Sub, 3)),
					Integer.parseInt(getSubToken(Sub, 4)),
					Integer.parseInt(getSubToken(Sub, 5)),
					Integer.parseInt(getSubToken(Sub, 6)));
			break;
		case 's': {
			sector = new Sector(Float.parseFloat(getSubToken(Sub, 1)),
					Float.parseFloat(getSubToken(Sub, 2)),
					Float.parseFloat(getSubToken(Sub, 3)),
					Float.parseFloat(getSubToken(Sub, 4)),
					Float.parseFloat(getSubToken(Sub, 5)),
					Float.parseFloat(getSubToken(Sub, 6)));
			sectorList.addElement(sector);
		}
			break;
		case 'a': {
			Actor actor = new Actor();
			people.addElement(actor);
			actor.Sector = Integer.parseInt(getSubToken(Sub, 1));
			actor.candelas = Integer.parseInt(getSubToken(Sub, 2));
		}
			break;
		}
	}
	// ------------------------------------------------------------------------------------------------------------
	private int plusOneIfNotZero(int Int ) {
		if ( Int != 0)
			return Int + 1;
		else
			return 0;
	}
}
